package com.haixiaoke.saas.enterprise.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.material.domain.MaterialPhoto;
import com.haixiaoke.saas.material.service.IMaterialPhotoService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图片素材Controller
 * 
 * @author panda
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/enterprise/material/photo")
public class MaterialPhotoController extends BaseController {
    @Autowired
    private IMaterialPhotoService materialPhotoService;

    /**
     * 查询图片素材列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MaterialPhoto materialPhoto) {
        startPage();
        List<MaterialPhoto> list = materialPhotoService.selectMaterialPhotoList(materialPhoto);
        return getDataTable(list);
    }

    /**
     * 导出图片素材列表
     */
    @Log(title = "图片素材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialPhoto materialPhoto) {
        List<MaterialPhoto> list = materialPhotoService.selectMaterialPhotoList(materialPhoto);
        ExcelUtil<MaterialPhoto> util = new ExcelUtil<MaterialPhoto>(MaterialPhoto.class);
        util.exportExcel(response, list, "图片素材数据");
    }

    /**
     * 获取图片素材详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(materialPhotoService.selectMaterialPhotoById(id));
    }

    /**
     * 新增图片素材
     */
    @Log(title = "图片素材", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestParam(value = "groupId") Long groupId,@RequestParam(value = "files") List<MultipartFile> files) {
        if (!TenantContext.getTenantId().equals(getLoginUser().getTenantId())) {
            return error("异常操作数据！");
        }
        if (files == null || files.isEmpty()) {
            return error("图片获取异常");
        }
        if (groupId == null) {
            return error("请选择素材组");
        }
        return toAjax(materialPhotoService.insertMaterialPhoto(groupId,files));
    }

    /**
     * 修改图片素材属于哪个素材组
     */
    @Log(title = "图片素材", businessType = BusinessType.UPDATE)
    @PutMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult edit(@RequestBody MaterialPhoto materialPhoto) {
        if (!TenantContext.getTenantId().equals(getLoginUser().getTenantId())) {
            return error("异常操作数据！");
        }
        if (materialPhoto.getPhotoIds() == null || materialPhoto.getPhotoIds().length == 0) {
            return error("请选择图片");
        }
        materialPhoto.setUpdateBy(getUsername());
        return toAjax(materialPhotoService.updateMaterialPhoto(materialPhoto));
    }

    /**
     * 删除图片素材
     */
    @Log(title = "图片素材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{photoIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] photoIds) {
        if (!TenantContext.getTenantId().equals(getLoginUser().getTenantId())) {
            return error("异常操作数据！");
        }
        if (photoIds == null || photoIds.length == 0) {
            return error("请选择数据");
        }
        MaterialPhoto photo = new MaterialPhoto();
        photo.setUpdateBy(getUsername());
        photo.setUpdateTime(DateUtil.date());
        photo.setPhotoIds(photoIds);
        return toAjax(materialPhotoService.softDeleteMaterialPhotoByIds(photo));
    }
}
